package com.su.song.dataapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class DataBass_MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_creat;
    private Button btn_delete;
    private TextView tv_desc;
    private String mDataBase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_bass_main);
        btn_creat = findViewById(R.id.btn_creat);
        btn_delete = findViewById(R.id.btn_delete);
        tv_desc = findViewById(R.id.tv_desc);
        btn_creat.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        //生成一个测试数据库的完整路径
        mDataBase = getFilesDir() + "/test.db";

    }

    @Override
    public void onClick(View v) {
        String desc = null;
        switch (v.getId()){
            case R.id.btn_creat:
                //创建或打开数据库
                SQLiteDatabase db = openOrCreateDatabase(mDataBase, Context.MODE_PRIVATE, null);
                desc = String.format("数据库%s创建%s",db.getPath(),(db!=null) ? "成功":"失败");
                tv_desc.setText(desc);
                break;
            case R.id.btn_delete:
                boolean result = deleteDatabase(mDataBase);
                desc = String.format("数据库%s删除%s",mDataBase,result ? "成功":"失败");
                tv_desc.setText(desc);
                break;
        }
    }
}